/*
 * File:    ServiceCache.java
 * Created: 15-Apr-2006
 * Version: $Id: //depot/bitgate/bsse/src/bitgate/util/service/ServiceCache.java#1 $
 *
 * COPYRIGHT (C) 2006, Bitgate Software, LLC. All Rights Reserved.
 *
 * software@bitgatesoftware.com
 */

package com.bitgate.util.service;

import static com.bitgate.util.debug.Debug.*;

import java.util.HashMap;

/**
 * Although this is a caching mechanism for <code>Service</code> objects, it does not use the <code>Cache</code> mechanism
 * of the system.  All class members are retrieved using the static default accessor function.
 *
 * @author Kenji Hollis &lt;kenji@bitgatesoftware.com&gt;
 * @version $Id: //depot/bitgate/bsse/src/bitgate/util/service/ServiceCache.java#1 $
 */
public class ServiceCache
{
    private static final ServiceCache _default = new ServiceCache();
    private final HashMap<String, Service> serviceHash;

    private ServiceCache()
    {
    	this.serviceHash = new HashMap<String, Service>(5);
    	
    	if (isDebugEnabled()) {
    		debug("Service Cache constructed.");
    	}
    }

    /**
     * Returns the default instance of the <code>ServiceCache</code>.
     *
     * @return <code>static final ServiceCache</code> default instance object.
     */
    public static final ServiceCache getDefault()
    {
    	return _default;
    }

    /**
     * Adds a service based on a serial ID.
     *
     * @param serialId The serial ID to attach the <code>Service</code> object to.
     * @param service The service object to add.
     */
    public void addService(String serialId, Service service)
    {
    	this.serviceHash.put(serialId, service);
    	
    	if (isDebugEnabled()) {
    		debug("Added service for serial '" + serialId + "'");
    	}
    }

    /**
     * Retrieves the <code>Service</code> object based on the serial ID specified.
     *
     * @param serialId The serial ID to look up.
     * @return <code>Service</code> object, <code>null</code> if not found.
     */
    public Service getService(String serialId)
    {
    	return this.serviceHash.get(serialId);
    }
}
